﻿using Gp.Scripts.Core;
using UnityEngine;

namespace GGp.Scripts.Units.Feedback.Animation {
    public class UnitPreAttackCall : StateMachineBehaviour {
        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo,
            int layerIndex) {
            if (!Validate(animator, out var model)) return;
            model.SetSelectingTrigger();
        }

        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo,
            int layerIndex) {
            if (!Validate(animator, out var model)) return;
            model.UnSetSelectingTrigger();
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo,
            int layerIndex) { }

        public override void OnStateMove(Animator animator, AnimatorStateInfo stateInfo,
            int layerIndex) { }


        private bool Validate(Animator animator, out UnitWeaponModel weaponModel) {
            weaponModel = null;
            if (!animator.TryGetComponent<HumanoidModel>(out var model) || model.weaponPivot == null ||
                model.weaponPivot.CurWeapon == null) {
                return false;
            }

            weaponModel = model.weaponPivot.CurWeapon;
            return true;
        }
    }
}